SQL UNIQUE Constraint
SQL UNIQUE Constraint:
The UNIQUE constraint ensures that all values in a column are different.
UNIQUE constraints provide a guarantee for uniqueness for a column or set of columns.
You can have many UNIQUE constraints per table.
SQL UNIQUE Constraint on CREATE TABLE:
CREATE TABLE Persons (ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);
UNIQUE constraint on multiple columns:
CREATE TABLE Persons (ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);
SQL UNIQUE Constraint on ALTER TABLE:
ALTER TABLE Persons ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);
DROP a UNIQUE Constraint:
ALTER TABLE Persons DROP INDEX UC_Person;